package com.wwj.admin.notice.controller;

import com.github.pagehelper.Page;
import com.wwj.core.wawa.entity.Banner;
import com.wwj.core.wawa.service.IBannerService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.Page2EasyUiData;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Auser on 2017/11/8.
 */
@Controller
@RequestMapping("/notice/banner")
public class BannerController extends BaseController{

    private final String INDEX = "/admin/notice/banner";
    @Resource
    private IBannerService bannerService;

    @RequestMapping(value={"","/","/index"})
    public String index(){
        return INDEX;
    }

    @RequestMapping("/listData.json")
    @ResponseBody
    public ReturnData list(String search,int offset,int limit)throws Exception{
        Map<String,Object> params = new HashMap<String,Object>();
        if(null != search && !"".equals(search)) params.put("search",search);
        params.put("limit",limit);
        params.put("offset",offset);
        Page<Banner> page = bannerService.findByPage(params,offset,limit);
        return new ReturnData().success(Page2EasyUiData.format(page));
    }


    @RequestMapping("/add")
    @ResponseBody
    public ReturnData add(Banner banner)throws Exception{
        bannerService.createBanner(banner);
        return new ReturnData().success("ok");
    }

    @RequestMapping("/update")
     @ResponseBody
     public ReturnData update(Banner banner) throws Exception {
            bannerService.updateBanner(banner);
            return new ReturnData().success("ok");
    }

    @RequestMapping("/findById/{id}")
    @ResponseBody
    public ReturnData findById(@PathVariable("id")int id){
        try {
            Banner banner = bannerService.findById(id);
            return new ReturnData().success(banner);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnData().failure("error");
        }
    }

    @RequestMapping(value = "/deleteById/",method = RequestMethod.POST)
    @ResponseBody
    public ReturnData deleteById(int id){
        try {
            bannerService.deleteBannerById(id);
            return new ReturnData().success("ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnData().failure("error");
        }
    }
}
